package com.iflytek.autofly.setting.biz.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.base.BaseFragment.OnExpandChangeListener;

public class MyLinearLayout extends LinearLayout {
    private LinearLayout mLabelLayout;
    private LinearLayout mLabelContainerLayout;
    private RadioButton mArrowRb;
    private LinearLayout mSubLayout;

    private boolean mNoSub = false;
    private boolean mExpandable = false;
    private boolean mIsExpanded = false;

    private OnExpandChangeListener mLabelListener;
    private OnExpandChangeListener mSubListener;

    public MyLinearLayout(Context context, int labelId, int subId) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(context);
        inflater.inflate(R.layout.my_linearlayout, this);

        mLabelContainerLayout = (LinearLayout) findViewById(R.id.label_container_ll);
        mArrowRb = (RadioButton) findViewById(R.id.expandable_arrow_rb);
        mLabelLayout = (LinearLayout) findViewById(R.id.label_ll);
        mLabelLayout.addView(inflater.inflate(labelId, null));

        mSubLayout = (LinearLayout) findViewById(R.id.sub_ll);

        // 默认折叠
        collapseView();

        if (subId != -1) {
            // 有子视图
            mNoSub = false;
            mExpandable = true;
            LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            mSubLayout.addView(inflater.inflate(subId, null), param);
            setClickable(true);
        } else {
            // 无子视图
            mNoSub = true;
            mExpandable = false;
            mArrowRb.setVisibility(View.INVISIBLE);
            mSubLayout.setVisibility(View.GONE);
            // 设置不可点击，防止发出声音
            setClickable(false);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        super.setClickable(clickable);
        if (mLabelLayout != null) {
            mLabelLayout.setClickable(!clickable);
        }
        if (mArrowRb != null) {
            mArrowRb.setClickable(!clickable);
        }
        if (mLabelContainerLayout != null) {
            mLabelContainerLayout.setClickable(clickable);
        }

    }

    public void expand() {
        if (!mNoSub && mExpandable && mSubLayout != null && !mIsExpanded) {
            mIsExpanded = true;
            expandView();
            if (mLabelListener != null) {
                mLabelListener.onExpandChanged(true);
            }
            if (mSubListener != null) {
                mSubListener.onExpandChanged(true);
            }
        }
    }

    public void collapse() {
        if (!mNoSub && mExpandable && mSubLayout != null && mIsExpanded) {
            mIsExpanded = false;
            collapseView();
            if (mLabelListener != null) {
                mLabelListener.onExpandChanged(false);
            }
            if (mSubListener != null) {
                mSubListener.onExpandChanged(false);
            }
        }
    }

    public boolean isExpanded() {
        return mIsExpanded;
    }

    public void setExpandChangeListener(OnExpandChangeListener labelListener,
                                        OnExpandChangeListener subListener) {
        mLabelListener = labelListener;
        mSubListener = subListener;
    }

    public void setLabelOnClickListener(OnClickListener onClickListener) {
        mLabelContainerLayout.setOnClickListener(onClickListener);
        //mLabelLayout.setOnClickListener(onClickListener);
    }

    public boolean isExpandable() {
        return mExpandable;
    }

    public void setExpandable(boolean isExpandable) {
        if (mNoSub && isExpandable) {
            return;
        }
        if (mExpandable != isExpandable) {
            mExpandable = isExpandable;
            if (!mExpandable) {
                collapse();
                mArrowRb.setVisibility(View.INVISIBLE);
                setClickable(false);
            } else {
                mArrowRb.setVisibility(View.VISIBLE);
                setClickable(true);
            }
        }
    }

    private void expandView() {
        mSubLayout.setVisibility(View.VISIBLE);
        mArrowRb.setChecked(true);
    }

    private void collapseView() {
        mSubLayout.setVisibility(View.GONE);
        mArrowRb.setChecked(false);
    }

}
